from django.shortcuts import render, redirect, reverse
from django.http import HttpResponseForbidden, JsonResponse
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views import View
from login.models import Application, User
from hotel.models import Hotel
from corp.models import Corp


# Create your views here.


class IndexView(LoginRequiredMixin, View):
    """管理员首页视图"""

    def get(self, request):
        nickname = request.session.get('nickname')
        if nickname:
            context = {
                'nickname': nickname,
            }
            return render(request, 'myAdmin/index.html', context)
        else:
            return HttpResponseForbidden('登录状态有误')


class WelcomeView(View):
    """欢迎页面视图"""

    def get(self, request):
        return render(request, 'myAdmin/welcome.html')


class RegisterApplicationView(View):
    """注册申请管理页面视图"""

    def get(self, request):
        return render(request, 'myAdmin/reg_appli.html')

    def post(self, request):
        # 处理注册申请
        pass


class GetApplicationsView(View):
    """获取注册申请视图"""

    def get(self, request):
        # 查询所有申请
        applications = Application.objects.filter(state=Application.PENDING)
        application_list = []
        for application in applications:
            # 获取状态字符串
            for state_item in Application.STATE_CHOICES:
                if application.state == state_item[0]:
                    state = state_item[1]

            application_item = {
                'id': application.id,
                'applicant_name': application.applicant.username,
                'identity': application.identity,
                'state': state,
            }
            application_list.append(application_item)
        return JsonResponse({
            'code': 0,
            'msg': '',
            'count': len(application_list),
            'data': application_list,
        })


class CheckApplicationView(View):
    """审核申请视图"""

    def post(self, request):
        # 获取前端参数
        id = request.POST.get('id')
        username = request.POST.get('username')
        identity = request.POST.get('identity')
        state = request.POST.get('state')

        identity = int(identity)

        # 查询数据库获取application
        application = Application.objects.get(id=id)

        # 判断state
        if state == '待受理':
            # 更新user身份
            user = User.objects.get(username=username)
            user.identity = identity
            user.save()
            if identity == User.HOTEL:
                # 创建对应hotel对象
                Hotel.objects.create(owner=user, name=user.nickname + ' hotel',
                                     address='default')
            elif identity == User.TOUR_CORP:
                # 创建对应corp对象
                Corp.objects.create(name=user.nickname + 'Corp', owner=user)

            # 更新application状态
            application.state = Application.SUCCESS
            application.save()

        return JsonResponse({
            'status': 'OK',
        })


class ComplaintsView(View):
    """投诉管理页面视图"""

    def get(self, request):
        return render(request, 'myAdmin/complaints.html')
